package com.zwy.Shopping.servlet_;

import com.zwy.Shopping.dao.UserDao;
import com.zwy.Shopping.dao.impl.UserDaoImpl;
import com.zwy.Shopping.model.User;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/signup")
public class SignupServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("username");
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String psw = request.getParameter("password");
        User user = new User();
        user.setAddress(address);
        user.setPassword(psw);
        user.setEmail(email);
        user.setMobile(mobile);
        user.setName(uname);
        UserDao userDao = new UserDaoImpl();
        boolean success = userDao.addUser(user);
        if(success){
            System.out.println("注册成功");
            request.setAttribute("message","注册成功");
            request.setAttribute("page","login.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }else{
            System.out.println("注册失败");
            request.setAttribute("message","注册失败");
            request.setAttribute("page","signup.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }
    }
}
